﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using DALHelper.Business;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace ResortManager
{
    /// <summary>
    /// Interaction logic for pgLookup.xaml
    /// </summary>
    public partial class pgLookup : Page
    {
        private static IList<string> ROOM_FORM_LIST = new List<string> { "Giường đôi", "Giường đơn" };
        private static IList<string> ROOM_LEVEL_LIST = new List<string> { "Thường", "Trung bình", "Sang", "Rất sang", "VIP" };

        private ObservableCollection<RoomCategory> m_dtRoomDataSrc;

        private DateTime m_dtFilterStartTime;
        public DateTime FilterStartTime
        {
            get { return m_dtFilterStartTime; }
            set
            {
                m_dtFilterStartTime = value;
                dgLookup.ItemsSource = GetRoomDataView();
            }
        }

        private DateTime m_dtFilterEndTime;
        public DateTime FilterEndTime
        {
            get { return m_dtFilterEndTime; }
            set
            {
                m_dtFilterEndTime = value;
                dgLookup.ItemsSource = GetRoomDataView();
            }
        }

        public ICollectionView GetRoomDataView()
        {
            if (FilterStartTime <= FilterEndTime)
            {
                m_dtRoomDataSrc = new ObservableCollection<RoomCategory>(
                           ((App)App.Current).BUSManager.Room.GetRoomFree(FilterStartTime, FilterEndTime));
            }
            else
                return null;

            ICollectionView view = CollectionViewSource.GetDefaultView(m_dtRoomDataSrc);
            view.Filter = SearchFilter;
            return view;
        }

        private bool m_bIsBooking;
        public bool IsBooking
        {
            get { return m_bIsBooking; }
            set
            {
                m_bIsBooking = value;
                colBookRoom.Visibility = m_bIsBooking ? Visibility.Visible : Visibility.Hidden;
            }
        }

        public pgLookup()
        {
            InitializeComponent();

            dtpStart.SelectedDate = dtpEnd.SelectedDate = DateTime.Today;
            IsBooking = false;
            dgLookup.ItemsSource = GetRoomDataView();
            cmbRoomForm.ItemsSource = ROOM_FORM_LIST;
            cmbRoomLevel.ItemsSource = ROOM_LEVEL_LIST;
        }

        public IEnumerable<RoomCategory> GetRoomCatList(out int iBookedCount)
        {
            iBookedCount = 0;
            List<RoomCategory> lsRet = new List<RoomCategory>();
            foreach(RoomCategory roomCat in m_dtRoomDataSrc)
            {
                if (roomCat.BookedGuestCount < 0 || roomCat.BookedGuestCount > roomCat.MaxCapacity)
                {
                    iBookedCount = -1;
                    break;
                }
                if(roomCat.BookedGuestCount != 0)
                {
                    iBookedCount += roomCat.BookedGuestCount;
                    lsRet.Add(roomCat);
                }
            }
            return lsRet.ToArray();

            //iBookedCount = 0;
            //RoomCategory[] arr = new RoomCategory[m_dtRoomDataSrc.Count];
            //m_dtRoomDataSrc.CopyTo(arr, 0);
            //foreach (RoomCategory roomCat in arr)
            //{
            //    iBookedCount += roomCat.BookedGuestCount;

            //    if (roomCat.BookedGuestCount < 0 || roomCat.BookedGuestCount > roomCat.AvailCount)
            //    {
            //        iBookedCount = -1;
            //        break;
            //    }
            //}

            //return arr;
        }

        public static IList GetSelectedItems(ComboBox cb)
        {
            IList ls = ((ListBox)cb.Template.FindName("lstBox", cb)).SelectedItems;
            return ls;
        }

        private bool SearchFilter(object sender)
        {
            RoomCategory roomCat = sender as RoomCategory;

            if (roomCat != null)
            {
                bool b1 = cmbRoomForm == null || !cmbRoomForm.IsEnabled 
                        || GetSelectedItems(cmbRoomForm).Contains(roomCat.RoomForm);
                bool b2 = cmbRoomLevel == null || !cmbRoomLevel.IsEnabled 
                        || GetSelectedItems(cmbRoomLevel).Contains(roomCat.RoomLevel);
                return b1 && b2;
            }

            return false;
        }

        private void dgLookup_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void cmbRoomForm_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ICollectionView view = (dgLookup.ItemsSource as ICollectionView);
            if (view != null)
            {
                view.Refresh();
            }
        }

        private void chkRoomForm_Unchecked(object sender, RoutedEventArgs e)
        {
            ICollectionView view = (dgLookup.ItemsSource as ICollectionView);
            if (view != null)
            {
                view.Refresh();
            }
        }
    }
}
